﻿using System;
using System.Linq; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class FastOutStock : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        private string FBillNo = "";    
        private string ID;
        public decimal NeedQty = 0;
        private string FInterID;

        public FastOutStock()
        {
            InitializeComponent();
        }

        public FastOutStock(string _ID)
        {
            InitializeComponent();
            this.ID=_ID;
        }

        private void FastOutStock_Load(object sender, EventArgs e)
        {
            this.tb_FBillNo.Focus();
            DataGridStyle();
            BinData();
        }

      public void  BinData()
        {
            DataSet ds = bjs.GetFastOutStockList();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            dataGrid1.DataSource = dt;
        }

      public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "FastOutStock";

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "内码";
            cl2.MappingName = "FInterID";
            cl2.Width = 0;
            dts.GridColumnStyles.Add(cl2); 

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "销售订单";
            cl1.MappingName = "SaleBillNo";
            cl1.Width = 120;
            cl1.NullText = "";
            dts.GridColumnStyles.Add(cl1);           

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "需求量";
            cl3.MappingName = "NeedQty";
            cl3.Width = 50;
            dts.GridColumnStyles.Add(cl3);             
            
            dataGrid1.TableStyles.Add(dts);           
        } 
              
        //返回
        private void button1_Click(object sender, EventArgs e)
        {
            Main main = new Main();
            main.Show();
            this.Hide();
        }

        //刷新
        private void button3_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }

        //明细
        private void button5_Click(object sender, EventArgs e)
        {
            if (FBillNo == null || FBillNo == "")
            {
                MessageBox.Show("销售订单不存在，请重新扫码", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else
            {
                FastOutStockDetail ice = new FastOutStockDetail(this.ID, FBillNo, FInterID);
                ice.Show();
                this.Hide();
            }
        }

        //提交
        private void button4_Click(object sender, EventArgs e)
        {
            FBillNo = this.tb_FBillNo.Text.Trim();
            string  NeedQtys = this.tb_NeedQty.Text.Trim();
            bool result = false;
            if (FBillNo == null || FBillNo == "")
            {
                MessageBox.Show("请选择销售订单号", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            }
            else if (NeedQtys == null || NeedQtys == "")
            {
                MessageBox.Show("请填写米数", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            }
            else
            {
                try
                {
                    NeedQty = decimal.Parse(this.tb_NeedQty.Text.Trim());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("请输入数字或小数", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    this.tb_NeedQty.Text = ""; 
                    return;
                }

                result = bjs.UpdateFastOutStock(FBillNo, NeedQty, FInterID);
                if (result)
                {
                    DataGridStyle();
                    BinData();
                    this.tb_FBillNo.Text = "";
                    this.tb_NeedQty.Text = "";
                }
                else {
                    this.tb_FBillNo.Text = "";
                    this.tb_NeedQty.Text = "";
                }
            }
        }

        //完成
        private void button3_Click_1(object sender, EventArgs e)
        {
            string l_txt = "";
            bool result = false;
            if (FBillNo == null || FBillNo == "")
            {
                MessageBox.Show("请选择要完成的快速出库单", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else {
                result = bjs.FinishFastOutStock(FBillNo, Program.userid,FInterID,out  l_txt);
                if (result == true)
                {
                    DataGridStyle();
                    BinData();
                }
                else {
                    MessageBox.Show("完成失败"+l_txt, "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                this.tb_FBillNo.Text = "";
                this.tb_NeedQty.Text = "";
                this.tb_FBillNo.Focus();
            }          

        }

      

        //校验字符只能输入小数和数字     

        private void tb_NeedQty_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != (char)('.'))
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_NeedQty.Text = "";
                e.Handled = true;
            }

            /*小数点只能输入一次*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_NeedQty.Text = "";
                e.Handled = true;
            }
            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_NeedQty.Text = "";
                e.Handled = true;
            }

            /*第一位是0，第二位必须为小数点*/
            if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_NeedQty.Text = "";
                e.Handled = true;
            }
        }

        private void tb_FBillNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                FBillNo = this.tb_FBillNo.Text;
                if (FBillNo == null || FBillNo == "")
                {
                    MessageBox.Show("销售订单为空", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    this.tb_NeedQty.Focus();
                }            
            }
        }

        private void tb_NeedQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
              string   NeedQty1 = this.tb_NeedQty.Text.ToString().Trim();
              if (NeedQty1 == null || NeedQty1 == "")
              {
                  MessageBox.Show("数量为空", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                  return;
              }
              else
              {
                  this.button4.Focus();
              }
            }
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FInterID = ((DataTable)(dataGrid1.DataSource)).Rows[index]["FInterID"].ToString();
            FBillNo = ((DataTable)(dataGrid1.DataSource)).Rows[index]["SaleBillNo"].ToString();
            NeedQty = decimal.Parse(((DataTable)(dataGrid1.DataSource)).Rows[index]["NeedQty"].ToString());
            this.tb_FBillNo.Text = FBillNo;
            this.tb_NeedQty.Text = NeedQty.ToString();
        }

               
    }
}